public class HashBuck {
    static class Node{
        int key;
        int val;
        public Node next;
        public  Node(int key,int val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node[] array;
    public int usedsize;
    public static final float DEFAULT_LOAD_FACTOR = 0.75f;

    public HashBuck(){
        array=new Node[10];
    }

    public void put(int key,int val){
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        Node node=new Node(key,val);
        node.next=array[index];
        array[index]=node;
        usedsize++;
        if(doLoadFactor()>DEFAULT_LOAD_FACTOR){
            resize();
        }
    }

    private float doLoadFactor(){
        return usedsize*1.0f/array.length;
    }

    private void resize(){
        Node[] newArray=new Node[2*array.length];
        for(int i=0;i<array.length;i++){
            Node cur=array[i];
            while(cur!=null){
                Node tmp=cur.next;
                int index=cur.key%newArray.length;
                cur.next=newArray[index];
                newArray[index]=cur;
                cur=tmp;
            }
        }
        array=newArray;
    }

    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while (cur != null) {
            if(cur.key == key) {
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }
}
